package com.seocoo.worker.user;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.seocoo.worker.R;
import com.seocoo.worker.WorkerAppContent;
import com.seocoo.worker.base.ImageLoaderEngine;
import com.seocoo.worker.base.ImageSelector;
import com.seocoo.worker.base.PermissionUtil;
import com.seocoo.worker.base.UtilStatus;
import com.seocoo.worker.base.entity.EUserInfoBean;
import com.seocoo.worker.base.widget.LtvRIvLayout;
import com.seocoo.worker.base.widget.LtvRIvYuanLayout;
import com.seocoo.worker.base.widget.LtvRtvLayout;
import com.seocoo.worker.user.model.UserModel;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.DialogBuilder;
import com.sfh.lib.ui.MVVMActivity;

import java.util.List;

public class UserInfoActivity extends MVVMActivity implements View.OnClickListener {

    public static void start(Context context) {
        context.startActivity(new Intent(context, UserInfoActivity.class));
    }

    private LtvRIvYuanLayout lvAvatar;
    private LtvRtvLayout lvNickName;
    private LtvRtvLayout lvPhoneNumber;
    private LtvRtvLayout lvLevel;
    private LtvRtvLayout lvAdeptDesc;
    private TextView tvExit;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_info_activity);
        UtilStatus.initStatus(this, Color.WHITE);
        initView();

    }

    private void initView() {
        lvAvatar = findViewById(R.id.lv_avatar);
        lvNickName = findViewById(R.id.lv_nick_name);
        lvPhoneNumber = findViewById(R.id.lv_phone_number);
        lvLevel = findViewById(R.id.lv_level);
        lvAdeptDesc = findViewById(R.id.lv_adept_desc);
        tvExit = findViewById(R.id.tv_exit);

        lvAvatar.getIvRight().setOnClickListener(this);
        lvNickName.getTvRight().setOnClickListener(this);

        tvExit.setOnClickListener(this);
        lvLevel.getTvRight().setOnClickListener(this);

        getViewModel(UserModel.class).getUserInfo();
    }

    @LiveDataMatch
    public void onUserInfo(EUserInfoBean infoBean) {
        if (infoBean == null) {
            return;
        }

        if (TextUtils.isEmpty(infoBean.getAvatar())) {
            lvAvatar.getIvRight().setImageResource(R.drawable.user_register);
        } else {
            new ImageLoaderEngine(infoBean.getAvatar()).error(R.drawable.user_register).placeholder(R.drawable.user_register).into(lvAvatar.getIvRight());
        }
        lvNickName.getTvRight().setText(infoBean.getNickName());
        String phoneNumber = infoBean.getPhoneNumber().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        lvPhoneNumber.getTvRight().setText(phoneNumber);
        lvLevel.getTvRight().setText(infoBean.getCraftsmanLevel());
        lvAdeptDesc.getTvRight().setText(infoBean.getAdeptDesc());

    }

    @Override
    public void onClick(View v) {
        if (v == lvAvatar.getIvRight()) {
            PermissionUtil.checkPermissions(this, "请允许访问相册与摄像头", 1011, new PermissionUtil.PermissionCallBack() {
                @Override
                public void onHasPermission() {
                    ImageSelector.create().count(1).showCamera(true).start(UserInfoActivity.this, 1010);
                }

                @Override
                public void onFailedPermission() {
                    showDialogToast("授权失败！");
                }
            }, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);

        } else if (v == lvNickName.getTvRight()) {
            UserEditNickNameActivity.start(this, lvNickName.getTvRight().getText().toString(), 1002);

        }else if (v== lvLevel.getTvRight()){
            UserGradeActivity.start(this);
        } else if (v == tvExit) {

            DialogBuilder builder = new DialogBuilder();
            builder.setMessage("确定退出当前账号吗？");
            builder.setOkListener((dialog, which) -> {
                dialog.dismiss();
                WorkerAppContent appContent = (WorkerAppContent) getApplication();
                appContent.onExit();
            });
            showDialog(builder);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 1010) {
                List<String> images = ImageSelector.onActivityResult(resultCode, data);

                if (images != null && images.size() > 0) {
                    getViewModel(UserModel.class).editAvatar(1010, images.get(0));
                }
            } else if (requestCode == 1002) {
                getViewModel(UserModel.class).getUserInfo();
            }
        }

    }

}
